package com.printer.sdk;

import android.content.Context;
import android.graphics.Bitmap;

import com.printf.utils.ImageUtil;

public class ImageHandleUtils {


    /**
     * 处理图片
     */
    public static Bitmap handleImage(Bitmap bitmap, int index, Context context) {

        if (index == 0) {//Floyd-Steinberg
            return ImageUtil.convertGreyImgByFloyd(bitmap,context,false);
//            return ImageHandleUtils.convertGreyImgByFloyd(bitmap, context);
        } else if (index == 1) {//JaJuNi
            return ImageHandleUtils.convertGreyImgByJaJuNi(bitmap, context);
        } else if (index == 2) {//Floyd-Steinberg 扇形抖动
            return ImageHandleUtils.convertGreyImgBySectorJitter(bitmap, context);
        } else if (index == 3) {//Shiau-Fan抖动 1
            return ImageHandleUtils.convertGreyImgByShiauFan1(bitmap, context);
        } else if (index == 4) {//Shiau-Fan抖动 2
            return ImageHandleUtils.convertGreyImgByShiauFan2(bitmap, context);
        } else if (index == 5) {//Stucki 抖动
            return ImageHandleUtils.convertGreyImgByStucki(bitmap, context);
        } else if (index == 6) {//Burkes 抖动
            return ImageHandleUtils.convertGreyImgByBurkes(bitmap, context);
        } else if (index == 7) {//Sierra 抖动 1
            return ImageHandleUtils.convertGreyImgbySierra1(bitmap, context);
        } else if (index == 8) {//Sierra 抖动 2
            return ImageHandleUtils.convertGreyImgbySierra2(bitmap, context);
        } else if (index == 9) {//Sierra 抖动 3
            return ImageHandleUtils.convertGreyImgbySierra3(bitmap, context);
        } else if (index == 10) {//Atkinson 抖动
            return ImageHandleUtils.convertGreyImgByAtkinson(bitmap, context);
        } else if (index == 11) {//全向误差扩散
            return ImageHandleUtils.convertGreyImgByPoint(bitmap, context);
        } else if (index == 12) {//Damera-Venkata和Evans
            return ImageHandleUtils.convertGreyImgByDameraEvans(bitmap,context);
        } else if (index == 13) {//垂直梯度
            return ImageHandleUtils.convertGreyImgByVerticalGradient(bitmap,context);
        } else if (index == 14) {//子块误差扩散 2 * 2
            return ImageHandleUtils.convertGreyImgBySubblockErrorDiffusion22(bitmap,context);
        } else if (index == 15) {//子块误差扩散 3 * 3
            return ImageHandleUtils.convertGreyImgBySubblockErrorDiffusion33(bitmap,context);
        }
        return bitmap;
    }


    /**
     * 处理图片的框架
     */
    public static Bitmap baseHandleImg(Bitmap img, Context context, HandlePixCallBack handlePixCallBack) {
        int threshold = 128;

        int width = img.getWidth();//获取位图的宽  
        int height = img.getHeight();//获取位图的高  

        int[] pixels = new int[width * height]; //通过位图的大小创建像素点数组  

        img.getPixels(pixels, 0, width, 0, 0, width, height);
        int[] gray = new int[height * width];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                int grey = pixels[width * i + j];
                int red = ((grey & 0x00FF0000) >> 16);
                int green = ((grey & 0x0000FF00) >> 8);
                int blue = (grey & 0x000000FF);
                gray[width * i + j] = (int) (0.29900 * red + 0.58700 * green + 0.11400 * blue); // 灰度转化公式;
            }
        }
        int e = 0;
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                int g = gray[width * i + j];
                if (g > threshold) {
                    pixels[width * i + j] = 0xffffffff;
                    e = g - 255;
                } else {
                    pixels[width * i + j] = 0xff000000;
                    e = g - 0;
                }

                if (handlePixCallBack != null) {
                    handlePixCallBack.callBack(gray, i, j, width, height, e);
                }
            }
        }
        Bitmap mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        mBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return mBitmap;
    }

    /**
     * 子块误差扩散 3 * 3
     */
    public static Bitmap convertGreyImgBySubblockErrorDiffusion33(Bitmap img, Context context) {
        int threshold = 128;

        int width = img.getWidth();//获取位图的宽  
        int height = img.getHeight();//获取位图的高  

        int[] pixels = new int[width * height]; //通过位图的大小创建像素点数组  

        img.getPixels(pixels, 0, width, 0, 0, width, height);
        int[] gray = new int[height * width];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                int grey = pixels[width * i + j];
                int red = ((grey & 0x00FF0000) >> 16);
                int green = ((grey & 0x0000FF00) >> 8);
                int blue = (grey & 0x000000FF);
                gray[width * i + j] = (int) (0.29900 * red + 0.58700 * green + 0.11400 * blue); // 灰度转化公式;
            }
        }
        int ea = 0;
        int eb = 0;
        int ec = 0;
        int ed = 0;
        int ee = 0;
        int ef = 0;
        int eg = 0;
        int eh = 0;
        int ei = 0;
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                int g = gray[width * i + j];

                if (j > 0 && j < width - 3 && i < height - 3) {
                    if (g > threshold) {
                        pixels[width * i + j] = 0xffffffff;
                        ea = g - 255;
                        eb = gray[width * i + j + 1] - 255;
                        ec = gray[width * (i) + j + 2] - 255;

                        ed = gray[width * (i + 1) + (j)] - 255;
                        ee = gray[width * (i + 1) + j + 1] - 255;
                        ef = gray[width * (i + 1) + j + 2] - 255;

                        eg = gray[width * (i + 2) + j] - 255;
                        eh = gray[width * (i + 2) + j + 1] - 255;
                        ei = gray[width * (i + 2) + j + 2] - 255;

                    } else {
                        pixels[width * i + j] = 0xff000000;
                        ea = g - 255;
                        eb = gray[width * i + j + 1];
                        ec = gray[width * (i) + j + 2];

                        ed = gray[width * (i + 1) + (j)];
                        ee = gray[width * (i + 1) + j + 1];
                        ef = gray[width * (i + 1) + j + 2];

                        eg = gray[width * (i + 2) + j];
                        eh = gray[width * (i + 2) + j + 1];
                        ei = gray[width * (i + 2) + j + 2];
                    }

                    //处理 像素 a
                    gray[width * i + (j + 3)] += ea / 32;//0,3
                    gray[width * (i + 1) + (j + 3)] += ea * 5 / 64;//1,3
                    gray[width * (i + 2) + (j + 3)] += ea * 3 / 32;//2,3

                    gray[width * (i + 3) + j - 1] += ea * 5 / 64;//3,-1
                    gray[width * (i + 3) + j] += ea * 17 / 64;//3,0
                    gray[width * (i + 3) + j + 1] += ea * 9 / 32;//3,1
                    gray[width * (i + 3) + j + 2] += ea / 8;//3,2
                    gray[width * (i + 3) + j + 3] += ea / 64;//3,3


                    //处理 像素 b
                    gray[width * i + (j + 3)] += eb * 3 / 32;//0,3
                    gray[width * (i + 1) + (j + 3)] += eb * 9 / 64;//1,3
                    gray[width * (i + 2) + (j + 3)] += eb / 8;//2,3

                    gray[width * (i + 3) + j - 1] += eb / 32;//3,-1
                    gray[width * (i + 3) + j] += eb * 11 / 64;//3,0
                    gray[width * (i + 3) + j + 1] += eb / 4;//3,1
                    gray[width * (i + 3) + j + 2] += eb * 11 / 64;//3,2
                    gray[width * (i + 3) + j + 3] += eb / 64;//3,3


                    //处理 像素 c
                    gray[width * i + (j + 3)] += ec * 5 / 16;//0,3
                    gray[width * (i + 1) + (j + 3)] += ec * 7 / 32;//1,3
                    gray[width * (i + 2) + (j + 3)] += ec / 8;//2,3

                    gray[width * (i + 3) + j] += ec * 3 / 64;//3,0
                    gray[width * (i + 3) + j + 1] += ec * 9 / 64;//3,1
                    gray[width * (i + 3) + j + 2] += ec * 9 / 64;//3,2
                    gray[width * (i + 3) + j + 3] += ec / 64;//3,3


                    //处理 像素 d
                    gray[width * (i + 1) + (j + 3)] += ed / 32;//1,3
                    gray[width * (i + 2) + (j + 3)] += ed * 5 / 64;//2,3

                    gray[width * (i + 3) + j - 1] += ed * 7 / 64;//3,-1
                    gray[width * (i + 3) + j] += ed * 23 / 64;//3,0
                    gray[width * (i + 3) + j + 1] += ed * 9 / 32;//3,1
                    gray[width * (i + 3) + j + 2] += ed / 8;//3,2
                    gray[width * (i + 3) + j + 3] += ed / 64;//3,3


                    //处理 像素 e
                    gray[width * (i + 1) + (j + 3)] += ee * 3 / 32;//1,3
                    gray[width * (i + 2) + (j + 3)] += ee * 9 / 64;//2,3

                    gray[width * (i + 3) + j - 1] += ee / 32;//3,-1
                    gray[width * (i + 3) + j] += ee * 3 / 16;//3,0
                    gray[width * (i + 3) + j + 1] += ee * 21 / 64;//3,1
                    gray[width * (i + 3) + j + 2] += ee * 13 / 64;//3,2
                    gray[width * (i + 3) + j + 3] += ee / 64;//3,3

                    //处理像素 f
                    gray[width * (i + 1) + (j + 3)] += ef * 5 / 16;//1,3
                    gray[width * (i + 2) + (j + 3)] += ef * 7 / 32;//2,3

                    gray[width * (i + 3) + j] += ef / 32;//3,0
                    gray[width * (i + 3) + j + 1] += ef * 11 / 64;//3,1
                    gray[width * (i + 3) + j + 2] += ef * 15 / 64;//3,2
                    gray[width * (i + 3) + j + 3] += ef / 32;//3,3

                    //处理像素 g
                    gray[width * (i + 1) + (j + 3)] += eg / 32;//1,3

                    gray[width * (i + 3) + j - 1] += eg * 3 / 16;//3,-1
                    gray[width * (i + 3) + j] += eg / 2;//3,0
                    gray[width * (i + 3) + j + 1] += eg * 7 / 32;//3,1
                    gray[width * (i + 3) + j + 2] += eg / 16;//3,2

                    //处理像素 h
                    gray[width * (i + 2) + (j + 3)] += eh * 3 / 32;//2,3

                    gray[width * (i + 3) + j] += eh * 3 / 16;//3,0
                    gray[width * (i + 3) + j + 1] += eh / 2;//3,1
                    gray[width * (i + 3) + j + 2] += eh * 13 / 64;//3,2
                    gray[width * (i + 3) + j + 3] += eh / 64;//3,3


                    //处理像素 i
                    gray[width * (i + 2) + (j + 3)] += ei * 5 / 16;//2,3

                    gray[width * (i + 3) + j + 1] += ei * 3 / 16;//3,1
                    gray[width * (i + 3) + j + 2] += ei * 7 / 16;//3,2
                    gray[width * (i + 3) + j + 3] += ei / 16;//3,3

                } else {
                    if (g > threshold) {
                        pixels[width * i + j] = 0xffffffff;
                    } else {
                        pixels[width * i + j] = 0xff000000;
                    }
                }

            }
        }
        Bitmap mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        mBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return mBitmap;
    }

    /**
     * 子块误差扩散 2 * 2
     */
    public static Bitmap convertGreyImgBySubblockErrorDiffusion22(Bitmap img, Context context) {

        int threshold = 128;

        int width = img.getWidth();//获取位图的宽  
        int height = img.getHeight();//获取位图的高  

        int[] pixels = new int[width * height]; //通过位图的大小创建像素点数组  

        img.getPixels(pixels, 0, width, 0, 0, width, height);
        int[] gray = new int[height * width];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                int grey = pixels[width * i + j];
                int red = ((grey & 0x00FF0000) >> 16);
                int green = ((grey & 0x0000FF00) >> 8);
                int blue = (grey & 0x000000FF);
                gray[width * i + j] = (int) (0.29900 * red + 0.58700 * green + 0.11400 * blue); // 灰度转化公式;
            }
        }
        int ea = 0;
        int eb = 0;
        int ec = 0;
        int ed = 0;
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                int g = gray[width * i + j];

                if (j > 0 && j < width - 2 && i < height - 2) {
                    if (g > threshold) {
                        pixels[width * i + j] = 0xffffffff;
                        ea = g - 255;
                        eb = gray[width * i + j + 1] - 255;
                        ec = gray[width * (i + 1) + j] - 255;
                        ed = gray[width * (i + 1) + (j + 1)] - 255;
                    } else {
                        pixels[width * i + j] = 0xff000000;
                        ea = g - 0;
                        eb = gray[width * i + j + 1];
                        ec = gray[width * (i + 1) + j];
                        ed = gray[width * (i + 1) + (j + 1)];
                    }

                    //处理 像素 a
                    gray[width * i + (j + 2)] += ea / 8;//0,2
                    gray[width * (i + 1) + (j + 2)] += ea * 5 / 32;//1,2

                    gray[width * (i + 2) + j - 1] += ea * 7 / 64;//2,-1
                    gray[width * (i + 2) + j] += ea * 11 / 32;//2,0
                    gray[width * (i + 2) + j + 1] += ea * 15 / 64;//2,1
                    gray[width * (i + 2) + j + 2] += ea / 32;//2,2


                    //处理 像素 b
                    gray[width * i + (j + 2)] += eb * 5 / 16;//0,2
                    gray[width * (i + 1) + (j + 2)] += eb * 7 / 32;//1,2

                    gray[width * (i + 2) + j - 1] += eb / 32;//2,-1
                    gray[width * (i + 2) + j] += eb * 11 / 64;//2,0
                    gray[width * (i + 2) + j + 1] += eb * 15 / 64;//2,1
                    gray[width * (i + 2) + j + 2] += eb / 32;//2,2


                    //处理 像素 c
                    gray[width * (i + 1) + (j + 2)] += eb * 3 / 32;//1,2

                    gray[width * (i + 2) + j - 1] += eb * 3 / 16;//2,-1
                    gray[width * (i + 2) + j] += eb / 2;//2,0
                    gray[width * (i + 2) + j + 1] += eb * 13 / 64;//2,1
                    gray[width * (i + 2) + j + 2] += eb / 64;//2,2


                    //处理 像素 d
                    gray[width * (i + 1) + (j + 2)] += eb * 5 / 16;//1,2

                    gray[width * (i + 2) + j - 1] += eb * 3 / 16;//2,-1
                    gray[width * (i + 2) + j] += eb * 7 / 16;//2,0
                    gray[width * (i + 2) + j + 1] += eb / 16;//2,1

                } else {
                    if (g > threshold) {
                        pixels[width * i + j] = 0xffffffff;
                    } else {
                        pixels[width * i + j] = 0xff000000;
                    }
                }

            }
        }
        Bitmap mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        mBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return mBitmap;
    }

    /**
     * 垂直梯度
     */
    public static Bitmap convertGreyImgByVerticalGradient(Bitmap img, Context context) {

        return baseHandleImg(img, context, new HandlePixCallBack() {
            @Override
            public void callBack(int[] gray, int i, int j, int width, int height, int e) {

            }
        });
    }

    /**
     * Damera-Venkata和Evans
     */
    public static Bitmap convertGreyImgByDameraEvans(Bitmap img, Context context) {

        return baseHandleImg(img, context, new HandlePixCallBack() {
            @Override
            public void callBack(int[] gray, int i, int j, int width, int height, int e) {


            }
        });
    }

    /**
     * 全向误差扩散
     */
    public static Bitmap convertGreyImgByPoint(Bitmap img, Context context) {
        return baseHandleImg(img, context, new HandlePixCallBack() {
            @Override
            public void callBack(int[] gray, int i, int j, int width, int height, int e) {
                if (i > 0 && j > 0 && j < width - 1 && i < height - 1) {

                    gray[width * (i - 1) + (j - 1)] += e / 10;//-1，-1
                    gray[width * (i - 1) + j] += e / 5;//-1,0
                    gray[width * (i - 1) + j + 1] += e / 10;

                    gray[width * i + (j - 1)] += e / 10;
                    gray[width * i + (j + 1)] += e / 10;

                    gray[width * (i + 1) + (j - 1)] += e / 10;
                    gray[width * (i + 1) + j] += e / 5;
                    gray[width * (i + 1) + (j + 1)] += e / 10;
                }
            }
        });
    }

    /**
     * Atkinson 抖动
     */
    public static Bitmap convertGreyImgByAtkinson(Bitmap img, Context context) {
        return baseHandleImg(img, context, new HandlePixCallBack() {
            @Override
            public void callBack(int[] gray, int i, int j, int width, int height, int e) {
                if (j > 0 && j < width - 2 && i < height - 2) {
                    gray[width * i + j + 1] += e / 8;//0 1
                    gray[width * i + j + 2] += e / 8;//0 2

                    gray[width * (i + 1) + j - 1] += e / 8;//1 -1
                    gray[width * (i + 1) + j] += e / 8;//1,0
                    gray[width * (i + 1) + j + 1] += e / 8;//1,1

                    gray[width * (i + 2) + j] += e / 8;//2,0
                }
            }
        });
    }

    /**
     * Sierra 抖动 3
     */
    public static Bitmap convertGreyImgbySierra3(Bitmap img, Context context) {
        return baseHandleImg(img, context, new HandlePixCallBack() {
            @Override
            public void callBack(int[] gray, int i, int j, int width, int height, int e) {
                if (j > 0 && j < width - 1 && i < height - 1) {
                    gray[width * i + j + 1] += e / 2;//0 1
                    gray[width * (i + 1) + j - 1] += e / 4;//1 -1
                    gray[width * (i + 1) + j] += e / 4;//1,0
                }
            }
        });
    }

    /**
     * Sierra 抖动 2
     */
    public static Bitmap convertGreyImgbySierra2(Bitmap img, Context context) {
        return baseHandleImg(img, context, new HandlePixCallBack() {
            @Override
            public void callBack(int[] gray, int i, int j, int width, int height, int e) {
                if (j > 1 && j < width - 2 && i < height - 1) {
                    gray[width * i + j + 1] += e / 4;//0 1
                    gray[width * i + j + 2] += e * 3 / 16;//0 2

                    gray[width * (i + 1) + j - 2] += e / 16;//1 -2
                    gray[width * (i + 1) + j - 1] += e / 8;//1 -1
                    gray[width * (i + 1) + j] += e * 3 / 16;//1,0
                    gray[width * (i + 1) + j + 1] += e / 8;//1,1
                    gray[width * (i + 1) + j + 2] += e / 16;//1,2
                }
            }
        });
    }

    /**
     * Sierra 抖动 1
     */
    public static Bitmap convertGreyImgbySierra1(Bitmap img, Context context) {
        return baseHandleImg(img, context, new HandlePixCallBack() {
            @Override
            public void callBack(int[] gray, int i, int j, int width, int height, int e) {
                if (j > 1 && j < width - 2 && i < height - 2) {
                    gray[width * i + j + 1] += e * 5 / 32;//0 1
                    gray[width * i + j + 2] += e * 3 / 32;//0 2

                    gray[width * (i + 1) + j - 2] += e / 16;//1 -2
                    gray[width * (i + 1) + j - 1] += e / 8;//1 -1
                    gray[width * (i + 1) + j] += e * 5 / 32;//1,0
                    gray[width * (i + 1) + j + 1] += e / 8;//1,1
                    gray[width * (i + 1) + j + 2] += e / 16;//1,2

                    gray[width * (i + 2) + j - 1] += e / 16;//2 -1
                    gray[width * (i + 2) + j] += e * 3 / 32;//2,0
                    gray[width * (i + 2) + j + 1] += e / 16;//2,1
                }
            }
        });
    }

    /**
     * Burkes 抖动
     */
    public static Bitmap convertGreyImgByBurkes(Bitmap img, Context context) {
        return baseHandleImg(img, context, new HandlePixCallBack() {
            @Override
            public void callBack(int[] gray, int i, int j, int width, int height, int e) {
                if (j > 1 && j < width - 2 && i < height - 1) {
                    gray[width * i + j + 1] += e / 4;//0 1
                    gray[width * i + j + 2] += e / 8;//0 2

                    gray[width * (i + 1) + j - 2] += e / 16;//1 -2
                    gray[width * (i + 1) + j - 1] += e / 8;//1 -1
                    gray[width * (i + 1) + j] += e / 4;//1,0
                    gray[width * (i + 1) + j + 1] += e / 8;//1,1
                    gray[width * (i + 1) + j + 2] += e / 16;//1,2
                }
            }
        });
    }

    /**
     * Stucki 抖动
     */
    public static Bitmap convertGreyImgByStucki(Bitmap img, Context context) {

        return baseHandleImg(img, context, new HandlePixCallBack() {
            @Override
            public void callBack(int[] gray, int i, int j, int width, int height, int e) {
                if (j > 1 && j < width - 2 && i < height - 2) {
                    gray[width * i + j + 1] += e * 4 / 21;//0 1
                    gray[width * i + j + 2] += e * 2 / 21;//0 2

                    gray[width * (i + 1) + j - 2] += e / 21;//1 -2
                    gray[width * (i + 1) + j - 1] += e * 2 / 21;//1 -1
                    gray[width * (i + 1) + j] += e * 4 / 21;//1,0
                    gray[width * (i + 1) + j + 1] += e * 2 / 21;//1,1
                    gray[width * (i + 1) + j + 2] += e / 21;//1,2

                    gray[width * (i + 2) + j - 2] += e / 42;//2 -2
                    gray[width * (i + 2) + j - 1] += e / 21;//2 -1
                    gray[width * (i + 2) + j] += e * 2 / 21;//2,0
                    gray[width * (i + 2) + j + 1] += e / 21;//2,1
                    gray[width * (i + 2) + j + 2] += e / 42;//2,2
                }
            }
        });
    }

    /**
     * Shiau-Fan 2 抖动
     */
    public static Bitmap convertGreyImgByShiauFan2(Bitmap img, Context context) {

        return baseHandleImg(img, context, new HandlePixCallBack() {
            @Override
            public void callBack(int[] gray, int i, int j, int width, int height, int e) {
                if (j > 2 && j < width - 1 && i < height - 1) {
                    gray[width * i + j + 1] += e / 2;//0,1
                    gray[width * (i + 1) + j - 3] += e / 16;
                    gray[width * (i + 1) + j - 2] += e / 16; //1,-2
                    gray[width * (i + 1) + j - 1] += e / 8;//1,-1
                    gray[width * (i + 1) + j] += e / 4;//1,0
                }
            }
        });
    }

    /**
     * Shiau-Fan 1 抖动
     */
    public static Bitmap convertGreyImgByShiauFan1(Bitmap img, Context context) {

        return baseHandleImg(img, context, new HandlePixCallBack() {
            @Override
            public void callBack(int[] gray, int i, int j, int width, int height, int e) {
                if (j > 1 && j < width - 1 && i < height - 1) {
                    gray[width * i + j + 1] += e / 2;//0,1
                    gray[width * (i + 1) + j - 2] += e / 8; //1,-2
                    gray[width * (i + 1) + j - 1] += e / 8;//1,-1
                    gray[width * (i + 1) + j] += e / 4;//1,0
                }
            }
        });
    }

    /**
     * Floyd-Steinberg 扇形抖动
     */
    public static Bitmap convertGreyImgBySectorJitter(Bitmap img, Context context) {

        return baseHandleImg(img, context, new HandlePixCallBack() {
            @Override
            public void callBack(int[] gray, int i, int j, int width, int height, int e) {
                if (j > 1 && j < width - 1 && i < height - 1) {
                    gray[width * i + j + 1] += e * 7 / 16;//0,1
                    gray[width * (i + 1) + j - 2] += e / 16; //1,-2
                    gray[width * (i + 1) + j - 1] += e * 3 / 16;//1,-1
                    gray[width * (i + 1) + j] += e * 5 / 16;//1,0
                }
            }
        });
    }

    /**
     * JaJuNi
     */
    public static Bitmap convertGreyImgByJaJuNi(Bitmap img, Context context) {
        return baseHandleImg(img, context, new HandlePixCallBack() {
            @Override
            public void callBack(int[] gray, int i, int j, int width, int height, int e) {
                if (j > 1 && j < width - 2 && i < height - 2) {
                    gray[width * i + j + 1] += e * 7 / 48;//0 1
                    gray[width * i + j + 2] += e * 5 / 48;//0 2

                    gray[width * (i + 1) + j - 2] += e / 16;//1 -2
                    gray[width * (i + 1) + j - 1] += e * 5 / 48;//1 -1
                    gray[width * (i + 1) + j] += e * 7 / 48;//1,0
                    gray[width * (i + 1) + j + 1] += e * 5 / 48;//1,1
                    gray[width * (i + 1) + j + 2] += e / 16;//1,2

                    gray[width * (i + 2) + j - 2] += e / 48;//2 -2
                    gray[width * (i + 2) + j - 1] += e / 16;//2 -1
                    gray[width * (i + 2) + j] += e * 5 / 48;//2,0
                    gray[width * (i + 2) + j + 1] += e / 16;//2,1
                    gray[width * (i + 2) + j + 2] += e / 48;//2,2
                }
            }
        });
    }

    /**
     * Floyd-Steinberg
     *
     * @param img
     * @return
     */
    public static Bitmap convertGreyImgByFloyd(Bitmap img, Context context) {

        int threshold = 128;

        int width = img.getWidth();//获取位图的宽  
        int height = img.getHeight();//获取位图的高  

        int[] pixels = new int[width * height]; //通过位图的大小创建像素点数组  

        img.getPixels(pixels, 0, width, 0, 0, width, height);
        int[] gray = new int[height * width];
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                int grey = pixels[width * i + j];
                int red = ((grey & 0x00FF0000) >> 16);
                int green = ((grey & 0x0000FF00) >> 8);
                int blue = (grey & 0x000000FF);
                gray[width * i + j] = (int) (0.29900 * red + 0.58700 * green + 0.11400 * blue); // 灰度转化公式;
            }
        }

        int e = 0;
        for (int i = 0; i < height; i++) {
            for (int j = 0; j < width; j++) {
                int g = gray[width * i + j];
                if (g > threshold) {
                    pixels[width * i + j] = 0xffffffff;
                    e = g - 255;
                } else {
                    pixels[width * i + j] = 0xff000000;
                    e = g - 0;
                }
                if (j < width - 1 && i < height - 1) {
                    //右边像素处理
                    gray[width * i + j + 1] += 3 * e / 8;
                    //下
                    gray[width * (i + 1) + j] += 3 * e / 8;
                    //右下
                    gray[width * (i + 1) + j + 1] += e / 4;
                } else if (j == width - 1 && i < height - 1) {//靠右或靠下边的像素的情况
                    //下方像素处理
                    gray[width * (i + 1) + j] += 3 * e / 8;
                } else if (j < width - 1 && i == height - 1) {
                    //右边像素处理
                    gray[width * (i) + j + 1] += 3 * e / 8;
                }
//                //处理四个角
//                if (i == 0 && j == 0) {//左上角 可以处理 右 右下 下
//                    //右
//                    gray[width * i + j + 1] += e * 7 / 16;
//                    //下
//                    gray[width * (i + 1) + j] += e * 5 / 16;
//                    //右下
//                    gray[width * (i + 1) + j + 1] += e * 1 / 16;
//                } else if (i == height - 1 && j == 0) {//左下角 只能处理 右
//                    gray[width * i + j + 1] += e * 7 / 16;
//                } else if (i == 0 && j == width - 1) {//右上角 处理 下 左下
//                    //下
//                    gray[width * (i + 1) + j] += e * 5 / 16;
//                    //左下
//                    gray[width * (i + 1) + j - 1] += 3 / 16 * e;
//                } else if (i == height - 1 && j == width - 1) {//右下角 没有可以处理的点
//
//                } else if (i == 0) {//如果靠在上边的点 可以处理全部
//                    //右
//                    gray[width * i + j + 1] += e * 7 / 16;
//                    //下
//                    gray[width * (i + 1) + j] += e * 5 / 16;
//                    //右下
//                    gray[width * (i + 1) + j + 1] += e * 1 / 16;
//                    //左下
//                    gray[width * (i + 1) + j - 1] += 3 / 16 * e;
//                } else if (i == height - 1) {//如果靠在下边的点 只能处理右
//                    gray[width * i + j + 1] += e * 7 / 16;
//                } else if (j == 0) {//如果靠在左边的点 处理 右 下 右下
//                    //右
//                    gray[width * i + j + 1] += e * 7 / 16;
//                    //下
//                    gray[width * (i + 1) + j] += e * 5 / 16;
//                    //右下
//                    gray[width * (i + 1) + j + 1] += e * 1 / 16;
//                } else if (j == width - 1) {//如果靠在右边的点 处理 下 左下
//                    //下
//                    gray[width * (i + 1) + j] += e * 5 / 16;
//                    //左下
//                    gray[width * (i + 1) + j - 1] += 3 / 16 * e;
//                } else {//剩余的点，处理全部
//                    //右
//                    gray[width * i + j + 1] += e * 7 / 16;
//                    //下
//                    gray[width * (i + 1) + j] += e * 5 / 16;
//                    //右下
//                    gray[width * (i + 1) + j + 1] += e * 1 / 16;
//                    //左下
//                    gray[width * (i + 1) + j - 1] += 3 / 16 * e;
//                }
            }
        }
        Bitmap mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        mBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
        return mBitmap;
    }


    public interface HandlePixCallBack {
        void callBack(int[] gray, int i, int j, int width, int height, int e);
    }

}
